<?php

namespace app\shop\controller;

// 框架基础控制类
use think\Controller;
use think\Request;

class Product extends Controller
{
    //构造函数
    public function __construct()
    {
        //手动把父类的方法获取过来
        parent::__construct();

        // check_cors_request();

        // 全局用户模型
        $this->BusinessModel = model('Business.Business');
        $this->RecordModel = model('Business.Record');
        $this->ProductModel = model('Product.Product');
        $this->CollectionModel = model('Business.Collection'); 
    }

    // 列表
    public function list()
    {
        $keyword = $this->request->param('keyword', '', 'trim');
        $where = [];
        if(!empty($keyword)){
            $where['name'] = ['like', "%$keyword%"];
        }
        $result =  $this->ProductModel->where($where)->order('id DESC')->paginate(10,false,['query' => Request::instance()->param()]);

        if( count($result) > 0)
        {
            $this->success('', null, $result);
        }else
        {
            $this->error('', null, []);
        }
    }

    // 详情
    public function info()
    {
        if($this->request->isPost())
        {
            $proid = $this->request->param('proid', 0, 'trim');

            $busid = $this->request->param('busid', 0, 'trim');

            $info =  $this->ProductModel->find($proid);
            
            // 判断产品详情是否有登陆，有的话就查询购物车数量
            if($busid > 0){
                $num = $this->CartModel->where('busid', $busid)->count();
            }

            $data = [
                'num' => isset($num) ? $num : null,
                'info' => $info
            ];

            if(!$info)
            {
                $this->error('当前商品不存在');
            }

            $this->success('', '', $data);
        }
    }
}